package com.vxdata.utils.modules.utils.case_8x;

import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.io.IOException;

/**
 * base64 转 MultipartFile
 */
public class Base64ToMultipart {
    public static MultipartFile base64ToMultipart(String base64) {
        try {
            String[] baseStrs = base64.split(",");

            BASE64Decoder decoder = new BASE64Decoder();
            byte[] b = new byte[0];
            b = decoder.decodeBuffer(baseStrs[1]);

            for(int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return new BASE64DecodedMultipartFile(b, baseStrs[0]);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
  
    //测试
    public static void main(String[] args) {
        String base = "";
        MultipartFile multipartFiles[] = new MultipartFile[2];
        multipartFiles[0] = Base64ToMultipart.base64ToMultipart(base);
        System.out.println(multipartFiles[0].getSize());
        
    }
}